package com.hepinghospital.user.ui.activity.home;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.hjq.shape.view.ShapeTextView;
import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseActivity;
import com.hepinghospital.user.base.BaseApplication;
import com.hepinghospital.user.ui.activity.userinfo.MyFamilyActivity;
import com.hepinghospital.user.ui.dialog.RulesDialogFragment;
import com.hepinghospital.user.ui.kotlin.DailyDetailActivity;
import com.hepinghospital.user.util.ImageLoaderHelper;
import com.hepinghospital.user.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.response.Doctor;
import com.rest.response.MyFamilyListResponse;
import com.rest.response.ScheduleResponse;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import jz.joyoung.robot.util.UIConstants;


/**
 * @Description: 门诊预约——确认预约
 * @Author: lzp
 * @CreateDate: 2023/3/29 9:16
 * @UpdateRemark: 更新说明
 */
public class DailyCheckDetailActivity extends BaseActivity {
    @BindView(R.id.img_head)
    ImageView imgHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_level)
    TextView tvLevel;
    @BindView(R.id.tv_deptName)
    TextView tvDeptName;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_recipel)
    ShapeTextView tvRecipel;
    @BindView(R.id.tv_tag)
    ShapeTextView tvTag;
    @BindView(R.id.tv_deptName2)
    TextView tvDeptName2;
    @BindView(R.id.tv_name2)
    TextView tvName2;
    @BindView(R.id.tv_date)
    TextView tvDate;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_waiting_num)
    TextView tvWaitingNum;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_patient_name)
    TextView tvPatientName;
    @BindView(R.id.checkbox)
    CheckBox checkbox;
    @BindView(R.id.btn_confirm)
    TextView btnConfirm;


    String numberId, time, scheduleTime, price, deptName, number;
    Doctor doc;
    private MyFamilyListResponse myFamily;

    @Override
    protected void initViews() {
        tvTitle.setText("预约");
        tvName.setText(doc.docName);
        tvDeptName.setText(deptName);
        tvLevel.setText(doc.jobTitleName);
        tvDeptName2.setText(deptName);
        tvName2.setText(doc.docName);
        tvPrice.setText("￥" + price);
        tvWaitingNum.setText(number + "号");
        ImageLoaderHelper.getInstance().GlideCircleImageLoader(DailyCheckDetailActivity.this, doc.thumbnailUrl, imgHead);
    }

    @Override
    protected void initData() {
        doc = (Doctor) getIntent().getSerializableExtra("obj");
        numberId = getIntent().getStringExtra("numberId");
        time = getIntent().getStringExtra("time");
        scheduleTime = getIntent().getStringExtra("scheduleTime");
        price = getIntent().getStringExtra("price");
        deptName = getIntent().getStringExtra("deptName");
        number = getIntent().getStringExtra("number");
        tvDate.setText(scheduleTime);
        tvTime.setText(time.substring(0, 5));
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_daily_check;
    }

    @OnClick(R.id.btn_family)
    void btn_family() {
        startActivityForResult(MyFamilyActivity.class, 1);
    }

    @OnClick(R.id.btn_confirm)
    void btn_confirm() {
        userSchedule();
    }

    @OnClick(R.id.btn_must_know)
    void btn_must_know() {
        showRulesDialogFragment();
    }

    private RulesDialogFragment mRulesDialogFragment;

    private void showRulesDialogFragment() {

        mRulesDialogFragment = RulesDialogFragment.newInstance("8");
        mRulesDialogFragment.setmListener(() -> mRulesDialogFragment.dismiss());
        mRulesDialogFragment.show(getSupportFragmentManager(), "mSchudeleDialogFragment");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        //data的返回值根据
        switch (requestCode) {
            case 1:
                myFamily = (MyFamilyListResponse) data.getSerializableExtra(UIConstants.OBJ_MyFamilyListResponse);
                tvPatientName.setText(myFamily.name);
                BaseApplication.currentFamilyId = myFamily.patientId;
                break;
            default:
                break;
        }
    }

    private void userSchedule() {
        if (TextUtils.isEmpty(tvPatientName.getText().toString())) {
            ToastUtil.showToast(mContext, "请先选择就诊人");
            return;
        }

        if (!checkbox.isChecked()) {
            ToastUtil.showToast(mContext, "请先阅读并勾选挂号须知");
            return;
        }
//        RestProxy.getInstance().registerHePing(numberId, "1574589003787902978", new Observer<ScheduleResponse>() {
        showLoadingDialog(mContext);
        RestProxy.getInstance().registerHePing(numberId, myFamily.patientId, new Observer<ScheduleResponse>() {
            @Override
            public void onSubscribe(Disposable d) {
                btnConfirm.setClickable(false);
            }

            @Override
            public void onNext(ScheduleResponse s) {
                dismissLoadingDialog();
                Bundle bundle = new Bundle();
                bundle.putString("outpId", s.scheduleId);
                startActivity(DailyDetailActivity.class, bundle);
                finish();
            }

            @Override
            public void onError(Throwable e) {
                dismissLoadingDialog();
                ToastUtil.onError(DailyCheckDetailActivity.this, e);
                btnConfirm.setClickable(true);
            }

            @Override
            public void onComplete() {
                btnConfirm.setClickable(true);
            }
        });
    }
}
